/**
 * 智慧小区管理系统 - 路由配置
 * @author Wu.Liang
 * @date 2024-01-01
 * @updated 2024-12-21 - 完善路由权限守卫，增强权限控制和用户体验
 */
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import { useUserStore } from '@/store/user'
import { usePermissionStore } from '@/store/permission'
import { useAppStore } from '@/store/app'

// 路由配置
const routes: RouteRecordRaw[] = [
    {
        path: '/login',
        name: 'Login',
        component: () => import('@/views/Login.vue'),
        meta: {
            title: '登录',
            hidden: true,
            requireAuth: false
        }
    },
    {
        path: '/property/company/register',
        name: 'PropertyCompanyRegister',
        component: () => import('@/views/property/company/PropertyCompanyRegister.vue'),
        meta: {
            title: '物业公司注册',
            hidden: true,
            requireAuth: false
        }
    },
    {
        path: '/property/company/register-success',
        name: 'PropertyCompanyRegisterSuccess',
        component: () => import('@/views/property/company/PropertyCompanyRegisterSuccess.vue'),
        meta: {
            title: '注册成功',
            hidden: true,
            requireAuth: false
        }
    },
    {
        path: '/404',
        name: 'NotFound',
        component: () => import('@/views/error/404.vue'),
        meta: {
            title: '页面不存在',
            hidden: true,
            requireAuth: false
        }
    },
    {
        path: '/403',
        name: 'Forbidden',
        component: () => import('@/views/error/403.vue'),
        meta: {
            title: '访问被拒绝',
            hidden: true,
            requireAuth: false
        }
    },
    {
        path: '/',
        redirect: '/dashboard'
    },
    {
        path: '/dashboard',
        name: 'Dashboard',
        component: () => import('@/layout/index.vue'),
        redirect: '/dashboard/index',
        meta: {
            title: '仪表盘',
            icon: 'dashboard',
            requireAuth: true
        },
        children: [
            {
                path: 'index',
                name: 'DashboardIndex',
                component: () => import('@/views/Dashboard.vue'),
                meta: {
                    title: '首页',
                    icon: 'dashboard',
                    keepAlive: true,
                    requireAuth: true
                }
            },
            {
                path: 'property',
                name: 'PropertyDashboard',
                component: () => import('@/views/PropertyDashboard.vue'),
                meta: {
                    title: '物业驾驶舱',
                    icon: 'dashboard',
                    keepAlive: true,
                    requireAuth: true,
                    breadcrumb: false
                }
            }
        ]
    },
    {
        path: '/user',
        name: 'UserManagement',
        component: () => import('@/layout/index.vue'),
        redirect: '/user/list',
        meta: {
            title: '用户管理',
            icon: 'el-icon-user',
            requiresAuth: true,

        },
        children: [
            {
                path: 'list',
                name: 'UserList',
                component: () => import('@/views/user/UserList.vue'),
                meta: {
                    title: '用户列表',
                    requiresAuth: true,

                }
            },
            {
                path: 'role',
                name: 'RoleList',
                component: () => import('@/views/user/RoleList.vue'),
                meta: {
                    title: '角色管理',
                    requiresAuth: true,

                }
            },
            {
                path: 'permission',
                name: 'PermissionList',
                component: () => import('@/views/user/PermissionList.vue'),
                meta: {
                    title: '权限管理',
                    requiresAuth: true,

                }
            }
        ]
    },
    {
        path: '/region',
        name: 'RegionManagement',
        component: () => import('@/layout/index.vue'),
        redirect: '/region/list',
        meta: {
            title: '区域管理',
            icon: 'el-icon-location',
            requiresAuth: true,

        },
        children: [
            {
                path: 'list',
                name: 'RegionList',
                component: () => import('@/views/region/RegionList.vue'),
                meta: {
                    title: '区域列表',
                    requiresAuth: true,

                }
            },
            {
                path: 'community',
                name: 'CommunityList',
                component: () => import('@/views/region/community/CommunityList.vue'),
                meta: {
                    title: '小区管理',
                    requiresAuth: true,

                }
            },
            {
                path: 'unit',
                name: 'UnitList',
                component: () => import('@/views/region/unit/UnitList.vue'),
                meta: {
                    title: '单元管理',
                    requiresAuth: true,

                }
            },
            {
                path: 'building',
                name: 'BuildingList',
                component: () => import('@/views/region/building/BuildingList.vue'),
                meta: {
                    title: '楼栋管理',
                    requiresAuth: true,

                }
            },
            {
                path: 'building/layout/:id',
                name: 'BuildingLayout',
                component: () => import('@/views/region/building/BuildingLayout.vue'),
                meta: {
                    title: '楼栋布局',
                    requiresAuth: true,

                    hidden: true
                }
            },
            {
                path: 'household',
                name: 'HouseholdList',
                component: () => import('@/views/region/household/HouseholdList.vue'),
                meta: {
                    title: '房户管理',
                    requiresAuth: true,

                }
            },
            {
                path: 'household/add',
                name: 'HouseholdAdd',
                component: () => import('@/views/region/household/HouseholdAdd.vue'),
                meta: {
                    title: '新增房户',
                    requiresAuth: true,

                    hidden: true
                }
            },
            {
                path: 'household/edit/:id',
                name: 'HouseholdEdit',
                component: () => import('@/views/region/household/HouseholdEdit.vue'),
                meta: {
                    title: '编辑房户',
                    requiresAuth: true,

                    hidden: true
                }
            },
            {
                path: 'resident',
                name: 'ResidentManage',
                component: () => import('@/views/region/resident/ResidentManage.vue'),
                meta: {
                    title: '住户管理',
                    requiresAuth: true,

                }
            },
            {
                path: 'resident/add',
                name: 'ResidentAdd',
                component: () => import('@/views/region/resident/ResidentAdd.vue'),
                meta: {
                    title: '新增住户',
                    requiresAuth: true,

                    hidden: true
                }
            },
            {
                path: 'resident/edit/:id',
                name: 'ResidentEdit',
                component: () => import('@/views/region/resident/ResidentEdit.vue'),
                meta: {
                    title: '编辑住户',
                    requiresAuth: true,

                    hidden: true
                }
            },
            {
                path: 'owner',
                name: 'OwnerList',
                component: () => import('@/views/region/owner/OwnerList.vue'),
                meta: {
                    title: '业主管理',
                    requiresAuth: true,

                }
            },
            {
                path: 'statistics',
                name: 'RegionStatistics',
                component: () => import('@/views/region/RegionStatistics.vue'),
                meta: {
                    title: '区域统计',
                    requiresAuth: true,

                }
            },
            {
                path: 'house-type',
                name: 'HouseTypeConfig',
                component: () => import('@/views/region/HouseTypeConfig.vue'),
                meta: {
                    title: '户型配置',
                    requiresAuth: true,

                }
            },
            {
                path: 'zone-management',
                name: 'ZoneManagement',
                component: () => import('@/views/region/ZoneManagement.vue'),
                meta: {
                    title: '分区管理',
                    requiresAuth: true,

                }
            },
            {
                path: 'building-type',
                name: 'BuildingTypeList',
                component: () => import('@/views/region/BuildingTypeList.vue'),
                meta: {
                    title: '建筑类型',
                    requiresAuth: true,

                }
            },
            {
                path: 'house-type',
                name: 'HouseTypeConfig',
                component: () => import('@/views/region/HouseTypeConfig.vue'),
                meta: {
                    title: '户型配置',
                    requiresAuth: true,

                }
            },
            {
                path: 'permission-test',
                name: 'PermissionTest',
                component: () => import('@/views/debug/PermissionTest.vue'),
                meta: {
                    title: '权限测试',
                    requiresAuth: true,
                    hidden: true
                }
            },
            {
                path: 'building-type-test',
                name: 'BuildingTypeTest',
                component: () => import('@/views/debug/BuildingTypeTest.vue'),
                meta: {
                    title: '建筑类型测试',
                    requiresAuth: true,
                    hidden: true
                }
            },
            {
                path: 'community/template-import',
                name: 'CommunityTemplateImport',
                component: () => import('@/views/region/community/TemplateImport.vue'),
                meta: {
                    title: '模板导入',
                    requiresAuth: true,

                }
            }
        ]
    },
    {
        path: '/security',
        name: 'SecurityManagement',
        component: () => import('@/layout/index.vue'),
        redirect: '/security/monitor',
        meta: {
            title: '安防管理',
            icon: 'el-icon-video-camera',
            requiresAuth: true,

        },
        children: [
            {
                path: 'device',
                name: 'SecurityDevice',
                component: () => import('@/views/security/Device.vue'),
                meta: {
                    title: '安防设备',
                    requiresAuth: true,

                }
            },
            {
                path: 'record',
                name: 'SecurityRecord',
                component: () => import('@/views/security/Record.vue'),
                meta: {
                    title: '安防记录',
                    requiresAuth: true,

                }
            },
            {
                path: 'visitor',
                name: 'SecurityVisitor',
                component: () => import('@/views/security/Visitor.vue'),
                meta: {
                    title: '访客管理',
                    requiresAuth: true,

                }
            },
            {
                path: 'monitor',
                name: 'SecurityMonitor',
                component: () => import('@/views/security/Monitor.vue'),
                meta: {
                    title: '监控中心',
                    requiresAuth: true,

                }
            },
            {
                path: 'access',
                name: 'AccessControl',
                component: () => import('@/views/security/AccessControl.vue'),
                meta: {
                    title: '门禁管理',
                    requiresAuth: true,

                }
            }
        ]
    },
    {
        path: '/property',
        name: 'PropertyManagement',
        component: () => import('@/layout/index.vue'),
        redirect: '/property/fee',
        meta: {
            title: '物业管理',
            icon: 'el-icon-house',
            requiresAuth: true,

        },
        children: [
            {
                path: 'fee',
                name: 'PropertyFee',
                component: () => import('@/views/property/Fee.vue'),
                meta: {
                    title: '物业费管理',
                    requiresAuth: true,

                }
            },
            {
                path: 'company',
                name: 'PropertyCompany',
                component: () => import('@/views/property/company/PropertyCompanyList.vue'),
                meta: {
                    title: '物业公司管理',
                    requiresAuth: true,

                }
            },

            {
                path: 'company/detail/:id',
                name: 'PropertyCompanyDetail',
                component: () => import('@/views/property/company/PropertyCompanyDetail.vue'),
                meta: {
                    title: '物业公司详情',
                    requiresAuth: true,

                    hidden: true
                }
            },
            {
                path: 'company/add-user/:id',
                name: 'AddPropertyUser',
                component: () => import('@/views/property/company/AddPropertyUser.vue'),
                meta: {
                    title: '新增物业用户',
                    requiresAuth: true,

                    hidden: true
                }
            },
            {
                path: 'info',
                name: 'PropertyInfo',
                component: () => import('@/views/property/PropertyInfo.vue'),
                meta: {
                    title: '物业信息',
                    requiresAuth: true,

                }
            },
            {
                path: 'sanitation',
                name: 'SanitationFee',
                component: () => import('@/views/property/Sanitation.vue'),
                meta: {
                    title: '卫生费管理',
                    requiresAuth: true,

                }
            },
            {
                path: 'gas',
                name: 'GasFee',
                component: () => import('@/views/property/Gas.vue'),
                meta: {
                    title: '燃气费管理',
                    requiresAuth: true,

                }
            },
            {
                path: 'utility',
                name: 'UtilityFee',
                component: () => import('@/views/property/Utility.vue'),
                meta: {
                    title: '水电费管理',
                    requiresAuth: true,

                }
            },
            {
                path: 'repair',
                name: 'RepairService',
                component: () => import('@/views/property/Repair.vue'),
                meta: {
                    title: '维修服务',
                    requiresAuth: true,

                }
            },

            {
                path: 'billing-method',
                name: 'BillingMethod',
                component: () => import('@/views/property/BillingMethod.vue'),
                meta: {
                    title: '计费方式',
                    requiresAuth: true,

                }
            },

        ]
    },
    {
        path: '/emergency',
        name: 'EmergencyManagement',
        component: () => import('@/layout/index.vue'),
        redirect: '/emergency/call',
        meta: {
            title: '紧急求助',
            icon: 'el-icon-phone',
            requiresAuth: true,

        },
        children: [
            {
                path: 'call',
                name: 'EmergencyCall',
                component: () => import('@/views/emergency/Call.vue'),
                meta: {
                    title: '紧急呼叫',
                    requiresAuth: true,

                }
            },
            {
                path: 'plan',
                name: 'EmergencyPlan',
                component: () => import('@/views/emergency/Plan.vue'),
                meta: {
                    title: '应急预案',
                    requiresAuth: true,

                }
            },
            {
                path: 'contact',
                name: 'EmergencyContact',
                component: () => import('@/views/emergency/Contact.vue'),
                meta: {
                    title: '紧急联系人',
                    requiresAuth: true,

                }
            }
        ]
    },
    {
        path: '/complaint',
        name: 'ComplaintManagement',
        component: () => import('@/layout/index.vue'),
        redirect: '/complaint/list',
        meta: {
            title: '投诉建议',
            icon: 'el-icon-chat-dot-round',
            requiresAuth: true,

        },
        children: [
            {
                path: 'list',
                name: 'ComplaintList',
                component: () => import('@/views/complaint/ComplaintList.vue'),
                meta: {
                    title: '投诉列表',
                    requiresAuth: true,

                }
            },
            {
                path: 'suggestion',
                name: 'SuggestionList',
                component: () => import('@/views/complaint/SuggestionList.vue'),
                meta: {
                    title: '建议列表',
                    requiresAuth: true,

                }
            },
            {
                path: 'statistics',
                name: 'ComplaintStatistics',
                component: () => import('@/views/complaint/ComplaintStatistics.vue'),
                meta: {
                    title: '统计分析',
                    requiresAuth: true,

                }
            }
        ]
    },
    {
        path: '/appointment',
        name: 'AppointmentManagement',
        component: () => import('@/layout/index.vue'),
        redirect: '/appointment/list',
        meta: {
            title: '服务预约',
            icon: 'el-icon-calendar',
            requiresAuth: true,

        },
        children: [
            {
                path: 'list',
                name: 'AppointmentList',
                component: () => import('@/views/appointment/AppointmentList.vue'),
                meta: {
                    title: '预约列表',
                    requiresAuth: true,

                }
            },
            {
                path: 'service',
                name: 'ServiceProject',
                component: () => import('@/views/appointment/ServiceList.vue'),
                meta: {
                    title: '服务项目',
                    requiresAuth: true,

                }
            },
            {
                path: 'schedule',
                name: 'ScheduleManagement',
                component: () => import('@/views/appointment/ScheduleList.vue'),
                meta: {
                    title: '排班管理',
                    requiresAuth: true,

                }
            }
        ]
    },
    {
        path: '/notification',
        name: 'NotificationManagement',
        component: () => import('@/layout/index.vue'),
        redirect: '/notification/list',
        meta: {
            title: '通知管理',
            icon: 'el-icon-bell',
            requiresAuth: true,

        },
        children: [
            {
                path: 'list',
                name: 'NotificationList',
                component: () => import('@/views/notification/NotificationList.vue'),
                meta: {
                    title: '通知列表',
                    requiresAuth: true,

                }
            },
            {
                path: 'template',
                name: 'NotificationTemplate',
                component: () => import('@/views/notification/TemplateList.vue'),
                meta: {
                    title: '通知模板',
                    requiresAuth: true,

                }
            },
            {
                path: 'push',
                name: 'PushRecord',
                component: () => import('@/views/notification/PushRecord.vue'),
                meta: {
                    title: '推送记录',
                    requiresAuth: true,

                }
            }
        ]
    },
    {
        path: '/payment',
        name: 'PaymentManagement',
        component: () => import('@/layout/index.vue'),
        redirect: '/payment/list',
        meta: {
            title: '支付管理',
            icon: 'el-icon-money',
            requiresAuth: true,

        },
        children: [
            {
                path: 'list',
                name: 'PaymentList',
                component: () => import('@/views/payment/PaymentList.vue'),
                meta: {
                    title: '支付记录',
                    requiresAuth: true,

                }
            },
            {
                path: 'config',
                name: 'PaymentConfig',
                component: () => import('@/views/payment/PaymentConfig.vue'),
                meta: {
                    title: '支付配置',
                    requiresAuth: true,

                }
            },
            {
                path: 'channel',
                name: 'PaymentChannel',
                component: () => import('@/views/payment/PaymentChannel.vue'),
                meta: {
                    title: '支付渠道',
                    requiresAuth: true,

                }
            }
        ]
    },
    {
        path: '/parking',
        name: 'ParkingManagement',
        component: () => import('@/layout/index.vue'),
        redirect: '/parking/lot',
        meta: {
            title: '停车管理',
            icon: 'el-icon-truck',
            requiresAuth: true,

        },
        children: [
            {
                path: 'lot',
                name: 'ParkingLot',
                component: () => import('@/views/parking/Lot.vue'),
                meta: {
                    title: '停车场管理',
                    requiresAuth: true,

                }
            },
            {
                path: 'space',
                name: 'ParkingSpace',
                component: () => import('@/views/parking/Space.vue'),
                meta: {
                    title: '车位管理',
                    requiresAuth: true,

                }
            },
            {
                path: 'vehicle',
                name: 'ParkingVehicle',
                component: () => import('@/views/parking/Vehicle.vue'),
                meta: {
                    title: '车辆管理',
                    requiresAuth: true,

                }
            },
            {
                path: 'record',
                name: 'ParkingRecord',
                component: () => import('@/views/parking/Record.vue'),
                meta: {
                    title: '停车记录',
                    requiresAuth: true,

                }
            },
            {
                path: 'charging-pile',
                name: 'ChargingPile',
                component: () => import('@/views/parking/ChargingPile.vue'),
                meta: {
                    title: '充电桩管理',
                    requiresAuth: true,

                }
            },
            {
                path: 'charging-category',
                name: 'ChargingPileCategory',
                component: () => import('@/views/parking/ChargingPileCategory.vue'),
                meta: {
                    title: '充电桩分类',
                    requiresAuth: true,

                }
            },
            {
                path: 'charging-record',
                name: 'ChargingRecord',
                component: () => import('@/views/parking/ChargingRecord.vue'),
                meta: {
                    title: '充电记录',
                    requiresAuth: true,

                }
            },
            {
                path: 'statistics',
                name: 'ParkingStatistics',
                component: () => import('@/views/parking/Statistics.vue'),
                meta: {
                    title: '停车统计',
                    requiresAuth: true,

                }
            }
        ]
    },
    {
        path: '/community',
        name: 'CommunityService',
        component: () => import('@/layout/index.vue'),
        redirect: '/community/notice',
        meta: {
            title: '小区管理',
            icon: 'el-icon-office-building',
            requiresAuth: true,

        },
        children: [
            {
                path: 'notice',
                name: 'CommunityNotice',
                component: () => import('@/views/community/Notice.vue'),
                meta: {
                    title: '通知公告',
                    requiresAuth: true,

                }
            },
            {
                path: 'activity',
                name: 'CommunityActivity',
                component: () => import('@/views/community/Activity.vue'),
                meta: {
                    title: '小区活动',
                    requiresAuth: true,

                }
            }
        ]
    },

    {
        path: '/data',
        name: 'DataAnalysis',
        component: () => import('@/layout/index.vue'),
        redirect: '/data/overview',
        meta: {
            title: '数据统计',
            icon: 'el-icon-data-analysis',
            requiresAuth: true,

        },
        children: [
            {
                path: 'overview',
                name: 'DataOverview',
                component: () => import('@/views/data/Overview.vue'),
                meta: {
                    title: '数据概览',
                    requiresAuth: true,

                }
            },
            {
                path: 'report',
                name: 'DataReport',
                component: () => import('@/views/data/Report.vue'),
                meta: {
                    title: '统计报表',
                    requiresAuth: true,

                }
            }
        ]
    },
    {
        path: '/device',
        name: 'DeviceManagement',
        component: () => import('@/layout/index.vue'),
        redirect: '/device/monitor',
        meta: {
            title: '设备管理',
            icon: 'el-icon-monitor',
            requiresAuth: true,

        },
        children: [
            {
                path: 'monitor',
                name: 'DeviceMonitor',
                component: () => import('@/views/device/Monitor.vue'),
                meta: {
                    title: '设备监控',
                    requiresAuth: true,

                }
            },
            {
                path: 'facility',
                name: 'DeviceFacility',
                component: () => import('@/views/device/Facility.vue'),
                meta: {
                    title: '设施管理',
                    requiresAuth: true,

                }
            },
            {
                path: 'maintenance',
                name: 'DeviceMaintenance',
                component: () => import('@/views/device/Maintenance.vue'),
                meta: {
                    title: '设备维护',
                    requiresAuth: true,

                }
            }
        ]
    },
    {
        path: '/system',
        name: 'SystemManagement',
        component: () => import('@/layout/index.vue'),
        redirect: '/system/config',
        meta: {
            title: '系统管理',
            icon: 'el-icon-setting',
            requiresAuth: true,

        },
        children: [
            {
                path: 'config',
                name: 'SystemConfig',
                component: () => import('@/views/system/Config.vue'),
                meta: {
                    title: '系统配置',
                    requiresAuth: true,

                }
            },
            {
                path: 'log',
                name: 'SystemLog',
                component: () => import('@/views/system/Log.vue'),
                meta: {
                    title: '操作日志',
                    requiresAuth: true,

                }
            },
            {
                path: 'login-log',
                name: 'LoginLog',
                component: () => import('@/views/system/LoginLog.vue'),
                meta: {
                    title: '登录日志',
                    requiresAuth: true,

                }
            },
            {
                path: 'permission-monitor',
                name: 'PermissionMonitor',
                component: () => import('@/views/system/PermissionMonitor.vue'),
                meta: {
                    title: '权限监控',
                    requiresAuth: true,

                }
            },
            {
                path: 'permission-test',
                name: 'PermissionTest',
                component: () => import('@/views/permission/PermissionTest.vue'),
                meta: {
                    title: '权限测试',
                    requiresAuth: true,

                }
            }
        ]
    },
    {
        path: '/profile',
        name: 'UserProfile',
        component: () => import('@/layout/index.vue'),
        redirect: '/profile/index',
        meta: {
            title: '个人中心',
            hideInMenu: true,
            requiresAuth: true
        },
        children: [
            {
                path: 'index',
                name: 'UserProfileIndex',
                component: () => import('@/views/profile/index.vue'),
                meta: {
                    title: '个人中心',
                    hideInMenu: true,
                    requiresAuth: true
                }
            },
            {
                path: 'change-password',
                name: 'ChangePassword',
                component: () => import('@/views/profile/ChangePassword.vue'),
                meta: {
                    title: '修改密码',
                    hideInMenu: true,
                    requiresAuth: true
                }
            }
        ]
    },
    {
        path: '/:pathMatch(.*)*',
        redirect: '/404'
    }
]

// 创建路由实例
const router = createRouter({
    history: createWebHistory(),
    routes,
    scrollBehavior: () => ({ left: 0, top: 0 })
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
    const userStore = useUserStore()
    const permissionStore = usePermissionStore()
    const appStore = useAppStore()

    // 设置页面加载状态
    appStore.showLoading('页面加载中...')

    // 设置页面标题
    document.title = to.meta?.title ? `${to.meta.title} - 智慧小区管理系统` : '智慧小区管理系统'

    // 白名单路由（不需要登录的页面）
    const whiteList = ['/login', '/404', '/403', '/property/company/register', '/property/company/register-success']

    if (whiteList.includes(to.path)) {
        appStore.hideLoading()
        next()
        return
    }

    // 等待应用初始化完成 - 新增
    if (!appStore.isReady) {
        console.log('应用未初始化完成，等待...')
        try {
            await appStore.waitForReady()
            console.log('应用初始化完成，继续路由跳转')
        } catch (error) {
            console.error('等待应用初始化超时:', error)
            // 超时后也继续执行，避免无限等待
        }
    }

    // 检查登录状态 - 强制跳转登录页
    if (to.meta?.requiresAuth !== false) {
        const token = localStorage.getItem('token')
        if (!userStore.isLogged || !token) {
            appStore.hideLoading()
            console.log('用户未登录，强制跳转到登录页:', { path: to.path, isLogged: userStore.isLogged, hasToken: !!token })
            // 保存当前路径，登录后跳转
            const redirect = encodeURIComponent(to.fullPath)
            next(`/login?redirect=${redirect}`)
            return
        }
    }

    // 检查Token有效性 - 强制跳转登录页
    if (to.meta?.requiresAuth !== false) {
        if (!userStore.checkTokenValidity()) {
            appStore.hideLoading()
            console.log('Token无效，强制跳转到登录页:', { path: to.path })
            // Token无效，跳转到登录页
            const redirect = encodeURIComponent(to.fullPath)
            next(`/login?redirect=${redirect}`)
            return
        }
    }

    // 权限简化：根据架构总览规范，仅保留菜单级权限，移除按钮级、方法级权限检查
    // 只保留基本的登录状态检查，不进行具体的权限点检查

    appStore.hideLoading()
    next()
})

router.afterEach((to) => {
    const appStore = useAppStore()
    const userStore = useUserStore()

    // 取消页面加载状态
    appStore.hideLoading()

    // 更新用户最后活跃时间
    if (userStore.isLogged) {
        userStore.updateLastActiveTime()
    }

    // 记录页面访问日志
    if (userStore.isLogged && to.meta?.title) {
        console.log('用户访问页面:', to.meta.title, '路径:', to.path)
    }
})

// 路由错误处理
router.onError((error) => {
    console.error('路由错误:', error)
    const appStore = useAppStore()
    appStore.hideLoading()
    appStore.setError('页面加载失败，请刷新重试')
})

// 导出路由
export default router

// 导出路由配置供其他地方使用
export { routes }
